//
//  PackageBannerCell.m
//  DingDongXiChe
//
//  Created by wtjr on 2017/11/27.
//  Copyright © 2017年 xieyingze. All rights reserved.
//

#import "PackageBannerCell.h"
#import "PackageCell.h"
#import "iCarousel.h"

@interface PackageBannerCell()<iCarouselDataSource, iCarouselDelegate>

@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) iCarousel *bannerView;

@end

@implementation PackageBannerCell

+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"PackageBannerCell";
    PackageBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[PackageBannerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

#pragma mark - Init

- (void)initSubView {
    [self addSubview:self.bannerView];
}

- (void)autoLayoutSubviews {
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
}

#pragma mark - Delegate

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return self.viewModel.packageList.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    PackageCell *packageCell = nil;
    if (!view) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240 * kUI_Percent, (kUI_HEIGHT - 88) * 1.0/3 - 30)];
        view.contentMode = UIViewContentModeCenter;
        packageCell = [PackageCell new];
        packageCell.model = self.viewModel.packageList[index];
        packageCell.tag = 1;
        [view addSubview:packageCell];
        [packageCell mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@0);
        }];
    } else {
        packageCell = (PackageCell *)[view viewWithTag:1];
    }
    return view;
}

- (NSInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel {
    return 2;
}

- (UIView *)carousel:(__unused iCarousel *)carousel placeholderViewAtIndex:(NSInteger)index reusingView:(UIView *)view {
    PackageCell *packageCell = nil;
    if (!view) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240 * kUI_Percent, (kUI_HEIGHT - 88) * 1.0/3 - 30)];
        view.contentMode = UIViewContentModeCenter;
        packageCell = [PackageCell new];
        packageCell.tag = 1;
        [view addSubview:packageCell];
        [packageCell mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@0);
        }];
    } else {
        packageCell = (PackageCell *)[view viewWithTag:1];
    }
    return view;
}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {
    //customize carousel display
    switch (option) {
        case iCarouselOptionWrap: {
            //normally you would hard-code this to YES or NO
            return YES;
        }
        case iCarouselOptionSpacing: {
            //add a bit of spacing between the item views
            return value * 1.05f;
        }
        default: {
            return value;
        }
    }
}

- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform {
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carousel.itemWidth);
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
    //NSLog(@"didSelectItemAtIndex = %ld",index);
}

- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel {
    //NSLog(@"carouselCurrentItemIndexDidChange: %@", @(carousel.currentItemIndex));
    if (self.clickCompleted) {
        self.clickCompleted(self.viewModel.packageList[carousel.currentItemIndex]);
    }
}


#pragma mark - Setter

- (void)setViewModel:(PackageViewModel *)viewModel {
    _viewModel = viewModel;
    [self.bannerView reloadData];
}


#pragma mark - Getter

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
    }
    return _mainView;
}

- (iCarousel *)bannerView {
    if (!_bannerView) {
        _bannerView = [iCarousel new];
        _bannerView.backgroundColor = kCOLOR_white;
        _bannerView.dataSource = self;
        _bannerView.delegate = self;
        _bannerView.type = iCarouselTypeCoverFlow2;
        _bannerView.layer.masksToBounds = YES;
    }
    return _bannerView;
}

@end
